home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  4.8 KB  |  215 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <stdio.h>
  14. #include <utmp.h>
  15. #include <grp.h>
  16.  
  17. #ifdef    BSD
  18. #include <strings.h>
  19. #define    strchr    index
  20. #else
  21. #include <string.h>
  22. #include <memory.h>
  23. #endif
  24.  
  25. #include "config.h"
  26. #include "pwd.h"
  27.  
  28. #ifdef    USE_SYSLOG
  29. #include <syslog.h>
  30.  
  31. #ifndef    LOG_WARN
  32. #define    LOG_WARN    LOG_WARNING
  33. #endif
  34. #endif
  35.  
  36. #ifndef    lint
  37. static    char    sccsid[] = "@(#)setup.c    3.11    12:21:39    3/8/92";
  38. #endif
  39.  
  40. #ifndef    SU
  41. extern    struct    utmp    utent;
  42. #endif
  43.  
  44. long    strtol ();
  45. #ifdef    HAVE_ULIMIT
  46. long    ulimit ();
  47. #endif
  48.  
  49. void    addenv ();
  50. extern    char    *getdef_str();
  51. extern    int    getdef_bool();
  52. extern    int    getdef_num();
  53.  
  54. /*
  55.  * setup - initialize login environment
  56.  *
  57.  *    setup() performs the following steps -
  58.  *
  59.  *    set the login tty to be owned by the new user ID with TTYPERM modes
  60.  *    change to the user's home directory
  61.  *    set the process nice, ulimit, and umask from the password file entry
  62.  *    set the group ID to the value from the password file entry
  63.  *    set the supplementary group IDs
  64.  *    set the user ID to the value from the password file entry
  65.  *    set the HOME, SHELL, MAIL, PATH, and LOGNAME or USER environmental
  66.  *    variables.
  67.  */
  68.  
  69. void    setup (info)
  70. struct    passwd    *info;
  71. {
  72.     extern    int    errno;
  73.     char    buf[BUFSIZ];
  74. #ifndef    SU
  75.     char    tty[sizeof utent.ut_line + 8];
  76. #endif
  77.     char    *cp;
  78.     char    *group;        /* TTY group name or number */
  79.     char    *maildir;    /* the directory in which the mailbox resides */
  80.     char    *mailfile;    /* the name of the mailbox */
  81.     struct    group    *grent;
  82.     int    i;
  83.     long    l;
  84.  
  85. #ifndef    SU
  86.     (void) strcat (strcpy (tty, "/dev/"), utent.ut_line);
  87.  
  88.     if (! (group = getdef_str ("TTYGROUP")))
  89.         i = info->pw_gid;
  90.     else if (group[0] >= '0' && group[0] <= '9')
  91.         i = atoi (group);
  92.     else if (grent = getgrnam (group))
  93.         i = grent->gr_gid;
  94.     else
  95.         i = info->pw_gid;
  96.  
  97.     if (chown (tty, info->pw_uid, i) ||
  98.             chmod (tty, getdef_num("TTYPERM", 0622))) {
  99.         (void) sprintf (buf, "Unable to change tty %s", tty);
  100. #ifdef    USE_SYSLOG
  101.         syslog (LOG_WARN, "unable to change tty `%s' for user `%s'\n",
  102.             tty, info->pw_name);
  103.         closelog ();
  104. #endif
  105.         perror (buf);
  106.         exit (errno);
  107.     }
  108. #endif
  109.     if (chdir (info->pw_dir) == -1) {
  110.         (void) sprintf (buf, "Unable to cd to \"%s\"", info->pw_dir);
  111. #ifdef    USE_SYSLOG
  112.         syslog (LOG_WARN, "unable to cd to `%s' for user `%s'\n",
  113.             info->pw_dir, info->pw_name);
  114.         closelog ();
  115. #endif
  116.         perror (buf);
  117.         exit (errno);
  118.     }
  119.     if ( getdef_bool("QUOTAS_ENAB") ) {
  120.         for (cp = info->pw_gecos ; cp != NULL ; cp = strchr (cp, ',')) {
  121.             if (*cp == ',')
  122.                 cp++;
  123.  
  124.             if (strncmp (cp, "pri=", 4) == 0) {
  125.                 i = atoi (cp + 4);
  126.                 if (i >= -20 && i <= 20)
  127.                     (void) nice (i);
  128.  
  129.                 continue;
  130.             }
  131. #ifdef    HAVE_ULIMIT
  132.             if (strncmp (cp, "ulimit=", 7) == 0) {
  133.                 l = strtol (cp + 7, (char **) 0, 10);
  134.                 (void) ulimit (2, l);
  135.  
  136.                 continue;
  137.             }
  138. #endif
  139.             if (strncmp (cp, "umask=", 6) == 0) {
  140.                 i = strtol (cp + 6, (char **) 0, 8) & 0777;
  141.                 (void) umask (i);
  142.  
  143.                 continue;
  144.             }
  145.         }
  146.     }
  147.     if (setgid (info->pw_gid) == -1) {
  148.         puts ("Bad group id");
  149. #ifdef    USE_SYSLOG
  150.         syslog (LOG_WARN, "bad group ID `%d' for user `%s'\n",
  151.             info->pw_gid, info->pw_name);
  152.         closelog ();
  153. #endif
  154.         exit (errno);
  155.     }
  156. #if NGROUPS > 1
  157.     if(initgroups(info->pw_name,info->pw_gid) == -1) {
  158.         puts ("initgroups failure");
  159. #ifdef    USE_SYSLOG
  160.         syslog (LOG_WARN, "initgroups failed for user `%s'\n",
  161.             info->pw_name);
  162.         closelog ();
  163. #endif
  164.         exit (errno);
  165.     }
  166. #endif /* NGROUPS > 1 */
  167. #ifndef    BSD
  168.     if (setuid (info->pw_uid))
  169. #else
  170.     if (setreuid (info->pw_uid, info->pw_uid))
  171. #endif
  172.     {
  173.         puts ("Bad user id");
  174. #ifdef    USE_SYSLOG
  175.         syslog (LOG_WARN, "bad user ID `%d' for user `%s'\n",
  176.             info->pw_uid, info->pw_name);
  177.         closelog ();
  178. #endif
  179.         exit (errno);
  180.     }
  181.     (void) strcat (strcpy (buf, "HOME="), info->pw_dir);
  182.     addenv (buf);
  183.  
  184.     if (info->pw_shell == (char *) 0 || ! *info->pw_shell)
  185.         info->pw_shell = "/bin/sh";
  186.  
  187.     (void) strcat (strcpy (buf, "SHELL="), info->pw_shell);
  188.     addenv (buf);
  189.  
  190.     cp = getdef_str( info->pw_uid == 0 ? "ENV_SUPATH" : "ENV_PATH" );
  191.     addenv( cp != NULL ? cp : "PATH=/bin:/usr/bin" );
  192.  
  193. #if defined(BSD) || defined(SUN) || defined(SUN4)
  194.     (void) strcat (strcpy (buf, "USER="), info->pw_name);
  195. #else
  196.     (void) strcat (strcpy (buf, "LOGNAME="), info->pw_name);
  197. #endif /* BSD || SUN || SUN4 */
  198.     addenv (buf);
  199.  
  200.     if ( (cp=getdef_str("MAIL_DIR")) != NULL ) {
  201.         maildir = cp;
  202.         mailfile = info->pw_name;
  203.     } else if ( (cp=getdef_str("MAIL_FILE")) != NULL) {
  204.         maildir = info->pw_dir;
  205.         mailfile = cp;
  206.     } else {
  207.         maildir = "/usr/spool/mail";
  208.         mailfile = info->pw_name;
  209.     }
  210.         
  211.     (void) strcat (strcat (strcat (strcpy (buf,
  212.         "MAIL="), maildir), "/"), mailfile);
  213.     addenv (buf);
  214. }
  215.